<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
	<style type="text/css">
		body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
		#l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
		#r-result{height:100%;width:20%;float:left;}
	</style>
	<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=pQFgFpS0VnMXwCRN6cTc1jDOcBVi3XoD"></script>
	<title>隐患地图</title>
</head>
<body>
	<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
//百度地图API功能
var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角，添加比例尺
var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件

// var map = new BMap.Map("allmap");
var map = new BMap.Map("allmap", {enableMapClick:false});//构造底图时，关闭底图可点功能

map.addControl(top_left_control);        
map.addControl(top_left_navigation); 

 
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
   }

 var lng=getQueryString("longitude");
 var lat=getQueryString("latitude");
 

 var point = new BMap.Point(lng, lat);
    map.centerAndZoom(point, 12);
    var marker = new BMap.Marker(point); // 创建点
	//map.addOverlay(marker);    //增加点
	var str = "我的位置";
	var opts = {
			   position : point,    // 指定文本标注所在的地理位置
			   offset   : new BMap.Size(-getByteLen(str)*3, 5)    //设置文本偏移量
			}	
	var label = new BMap.Label(str, opts);  // 创建文本标注对象
	label.setStyle({
					 fontSize : "12px",
					 height : "20px",
					 lineHeight : "20px",
					 fontFamily:"微软雅黑"
				  });
    map.addOverlay(label);   
    
    map.enableScrollWheelZoom(true);
    var geoc = new BMap.Geocoder();  

    //获取标注位置信息    
    function getAttr(){
		var p = marker.getPosition();       //获取marker的位置
		alert("marker的位置是" + p.lng + "," + p.lat);   
	}
 
    
    var xhm=new XMLHttpRequest();
    xhm.open("GET","../../baiduMap/getByPoint.do?limit=1000&offset=0&lng="+lng+"&lat="+lat+"&distance=5000",false);
    xhm.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xhm.send();
    var ticket=JSON.parse(xhm.responseText);
    console.log(ticket);
    addDeck(ticket);
 
function addDeck(ticket){	

// console.log(ticket);
var json_data = new Array();

for(var i=0;i<ticket.length;i++){
	json_data[i]=[ticket[i].lng,ticket[i].lat,
	              ticket[i].name,ticket[i].hidden_level];
}

var pointArray = new Array();
for(var i=0;i<json_data.length;i++){
	var point=new BMap.Point(json_data[i][0], json_data[i][1]);
	var myIcon = new BMap.Icon("../../img/hiddenPoint2.png", new BMap.Size(10,30));
	var marker= new BMap.Marker(point, {icon : myIcon}); // 创建点
	if(json_data[i][3]=="重大隐患"){
		var myIcon = new BMap.Icon("../../img/hiddenPoint0.png", new BMap.Size(10,30));
		marker = new BMap.Marker(point, {icon : myIcon}); // 创建点
	}else if(json_data[i][3]=="一级隐患"){
		var myIcon = new BMap.Icon("../../img/hiddenPoint1.png", new BMap.Size(10,30));
		marker = new BMap.Marker(point, {icon : myIcon}); // 创建点
	}else if(json_data[i][3]=="二级隐患"){
		var myIcon = new BMap.Icon("../../img/hiddenPoint2.png", new BMap.Size(10,30));
		marker = new BMap.Marker(point, {icon : myIcon}); // 创建点
	}
	map.addOverlay(marker);    //增加点
	/*
	var str = json_data[i][2];
	var opts = {
			   position : point,    // 指定文本标注所在的地理位置
			   offset   : new BMap.Size(-getByteLen(str)*4, 5)    //设置文本偏移量
			}	
	console.log(getByteLen(str));
	var label = new BMap.Label(str, opts);  // 创建文本标注对象
	label.setStyle({
					 color : "red",
					 fontSize : "12px",
					 height : "20px",
					 lineHeight : "20px",
					 fontFamily:"微软雅黑"
				  });
    map.addOverlay(label);
    */
	marker.addEventListener("click",attribute);
}
//让所有点在视野范围内
map.setViewport(pointArray);

}

//获取覆盖物位置
function attribute(e){
	var p = e.target;
    var hidden_lng=p.getPosition().lng;
    var hidden_lat=p.getPosition().lat;
    
    $.get("../../baiduMap/getGUIDByPosition.do",{
    	lng:hidden_lng,
    	lat:hidden_lat
    }, function(data){
    	var obj=$.parseJSON(data);
    	location.href="hidden/hiddenDetail.html?guid="+obj.guid+"&latitude="+lat+"&longitude="+lng;
    });
    
}	


function getByteLen(val) {    //传入一个字符串
    var len = 0;
    for (var i = 0; i < val.length; i++) {
        if (val[i].match(/[^\x00-\xff]/ig) != null) //全角 
            len += 2; //如果是全角，占用两个字节  如果mysql中某字段是text, 如果设置编码为utf-8,那么一个中文是占3个字节, gbk是两个字节
        else
            len += 1; //半角占用一个字节
    }
    return len;
 }       

</script>
